package edu.fafu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.fafu.entity.Answer;
import edu.fafu.entity.Question;
import edu.fafu.entity.User;
import edu.fafu.service.IAnswerService;
import edu.fafu.service.IQuestionService;
import edu.fafu.util.PageBean;

@Controller
public class QuestionAction extends ActionSupport implements RequestAware,SessionAware{

	@Resource(name="questionService")
	private IQuestionService questionService;
	@Resource(name="answerService")
	private IAnswerService answerService;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String addQuestion() throws Exception{
		question.setUserId((User)session.get("user"));
		questionService.addQuestion(question);
		request.put("msg","添加成功");
		return SUCCESS;
	}
	public String updateQuestion() throws Exception{
		questionService.updateQuestion(question);
		request.put("msg","修改成功");
		return SUCCESS;
	}
	public String query(){		
		PageBean pb = questionService.queryAllQuestion(pageBean);
		request.put("pageBean",pb);
		return SUCCESS;
	}
	public String queryOne(){
		question = questionService.queryQuestionOne(question.getId());
		request.put("question",question);
		return SUCCESS;
	}
	public String queryLike(){
		PageBean pb = questionService.queryQuestion(condition,pageBean);
		request.put("pageBean", pb);
		return SUCCESS;
	}
	public String deleteQuestion(){	
		questionService.deleteQuestion(question.getId());
		request.put("msg","删除成功" );
		return SUCCESS;
	}
	public String queryAnswer(){
		PageBean pb = questionService.queryAnswer(question,pageBean);
		request.put("pageBean", pb);
		request.put("questionId", question.getId());
		return SUCCESS;
	}
	public String addAnswer() throws Exception{
		Question q = questionService.queryQuestionOne(question.getId());
		answer.setUserId((User)session.get("user"));
		answerService.addAnswer(answer,q);
		request.put("msg","添加成功");
		request.put("pageBean", questionService.queryAnswer(q,pageBean));
		return SUCCESS;
	}
	public String personalAnswer(){
		User user = (User)session.get("user");
		PageBean pb = answerService.queryAnswerByUser(user.getId(), pageBean);
		request.put("pageBean",pb);
		return SUCCESS;
	}
	public String personalQuestion(){
		User user = (User)session.get("user");
		PageBean pb = questionService.queryQuestionByUser(user.getId(),pageBean);
		request.put("pageBean",pb);
		return SUCCESS;
	}
	public String addLike() {
		User user = (User)session.get("user");
		Answer answer1 = answerService.queryAnswerOneById(Integer.parseInt(answerId));
		Set set = answer1.getLikes();	
		set.add(user);
		answer1.setLikes(set);		
		try {			
			answerService.updateAnswer(answer1);		
		} catch (Exception e) {
			msg="每个答案只能点一次赞";
		}	
		answer = answerService.queryAnswerOneById(Integer.parseInt(answerId));
		return SUCCESS;

	}
	
	
	private PageBean pageBean=new PageBean();
	private String msg;
	private Question question;
	private Answer answer;
	private Map<String,Object> request;
	private String condition;
	private Map<String,Object> session;
	private String answerId;
	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getAnswerId() {
		return answerId;
	}

	public void setAnswerId(String answerId) {
		this.answerId = answerId;
	}

	public Answer getAnswer() {
		return answer;
	}

	public void setAnswer(Answer answer) {
		this.answer = answer;
	}

	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public Question getQuestion() {
		return question;
	}
	public void setQuestion(Question question) {
		this.question = question;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}
	
}
